package cn.tedu.personal_patient.information.controller;


import cn.tedu.common.pojo.authentication.CurrentPrincipal;
import cn.tedu.common.validation.account.UserRules;
import cn.tedu.common.web.JsonResult;
import cn.tedu.personal_patient.information.pojo.param.*;
import cn.tedu.personal_patient.information.pojo.vo.UserVO;
import cn.tedu.personal_patient.information.services.IUserService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Pattern;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/users")
@Validated
@Api(tags = "1. 用户个人信息")
public class UserController {
    @Autowired
    private IUserService userService;

    @GetMapping("")
    @ApiOperation("查询用户个人信息")
    @ApiOperationSupport(order = 310)
    public JsonResult listById(@AuthenticationPrincipal @ApiIgnore CurrentPrincipal currentPrincipal){
        List<UserVO> list = userService.listById(currentPrincipal);
        return JsonResult.ok(list);
    }

   /* @PostMapping("/{id}/nackname/update")
    @ApiOperation("修改昵称")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "nackname", value = "用户昵称", required = true, paramType = "string"),
            @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "long")
    })
    public JsonResult updatenNackName(@PathVariable @Range(min = 1, message = "请提交合法的用户ID值！") Long id,
                                      @NotBlank String nackname) {
        userService.updatenNackName(id, nackname);
        return JsonResult.ok();
    }

    @PostMapping("/{id}/imgeUrl/update")
    @ApiOperation("修改头像")
    @ApiOperationSupport(order = 320)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "imgeUrl", value = "新头像的路径", required = true, paramType = "query"),
            @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "long")
    })
    public JsonResult updateImgeUrl(@PathVariable @Range(min = 1, message = "请提交合法的用户ID值！") Long id,
                                   @NotBlank(message = "请提交新头像的路径") String imgeUrl) {
        userService.updateImgeUrl(id, imgeUrl);
        return JsonResult.ok();
    }

    @PostMapping("/{id}/phone/update")
    @ApiOperation("修改手机号码")
    @ApiOperationSupport(order = 330)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "手机号码", required = true, paramType = "query"),
            @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "long")
    })
    public JsonResult updatePhone(@PathVariable @Range(min = 1, message = "请提交合法的用户ID值！") Long id,
                                  @Pattern(regexp = UserRules.PATTERN_PHONE, message = UserRules.MESSAGE_PHONE_PATTERN) String phone) {
        userService.updatePhone(id, phone);
        return JsonResult.ok();
    }

    @PostMapping("/{id}/email/update")
    @ApiOperation("修改电子邮箱")
    @ApiOperationSupport(order = 340)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "email", value = "电子邮箱", required = true, paramType = "query"),
            @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "long")
    })
    public JsonResult updateEmail(@PathVariable @Range(min = 1, message = "请提交合法的用户ID值！") Long id,
                                  @Pattern(regexp = UserRules.PATTERN_EMAIL, message = UserRules.MESSAGE_EMAIL_PATTERN) String email) {
        userService.updateEmail(id, email);
        return JsonResult.ok();
    }*/

    @PostMapping("update")
    public JsonResult update(@AuthenticationPrincipal @ApiIgnore CurrentPrincipal currentPrincipal, UserParam userParam){
        log.error(currentPrincipal.getId()+"亿亿亿亿"+currentPrincipal.getUsername()+"亿亿亿亿"+userParam);
        userService.updateById(currentPrincipal,userParam);
        return JsonResult.ok();
    }

    @PostMapping("avatar")
    public JsonResult avatar(@AuthenticationPrincipal @ApiIgnore CurrentPrincipal currentPrincipal, UserAvatarParam userAvatarParam){

        System.out.println("currentPrincipal = " + currentPrincipal + ", staffAvatarParam = !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" + userAvatarParam);
        userService.avatar(currentPrincipal,userAvatarParam);
        return JsonResult.ok();
    }

}
